package cn.com.blueInfo.bpm.person.service.impl;

import cn.com.blueInfo.bpm.person.convert.HeadshipConvert;
import cn.com.blueInfo.bpm.person.entity.Headship;
import cn.com.blueInfo.bpm.person.repository.HeadshipRepository;
import cn.com.blueInfo.bpm.person.request.HeadshipDTO;
import cn.com.blueInfo.bpm.person.response.HeadshipVO;
import cn.com.blueInfo.bpm.person.service.HeadshipService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 职务信息表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:28:09
 */
@Log4j2
@Service
public class HeadshipServiceImpl implements HeadshipService {

    @Autowired
    private HeadshipRepository headshipRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private HeadshipConvert headshipConvert;

    /**
     * 新增职务信息
     * @param headshipDTO 职务信息参数
     */
    @Override
    public ResultInfo<Integer> addHeadship(HeadshipDTO headshipDTO) {
        Headship headship = headshipConvert.dtoToEntity(headshipDTO);
        headship.setId(snowflakeIdGenerator.nextId());
        Integer addSize = headshipRepository.createHeadship(headship);
        return ResultInfo.<Integer>builder().data(addSize).message("职务信息成功新增 {} 条").build();
    }

    /**
     * 批量新增职务信息
     * @param headshipDTOList 职务信息参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddHeadship(List<HeadshipDTO> headshipDTOList) {
        List<Headship> headshipList = headshipConvert.dtoToEntity(headshipDTOList);
        for (Headship headship : headshipList) headship.setId(snowflakeIdGenerator.nextId());
        Integer addSize = headshipRepository.batchCreateHeadship(headshipList);
        return ResultInfo.<Integer>builder().data(addSize).message("职务信息成功新增 {} 条").build();
    }

    /**
     * 删除职务信息
     * @param headshipId 职务信息ID
     */
    @Override
    public ResultInfo<Integer> removeHeadship(Long headshipId) {
        Integer removeSize = headshipRepository.deleteHeadship(headshipId);
        return ResultInfo.<Integer>builder().data(removeSize).message("职务信息成功删除 {} 条").build();
    }

    /**
     * 批量删除职务信息
     * @param headshipIdList 职务信息ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveHeadship(List<Long> headshipIdList) {
        Integer removeSize = headshipRepository.batchDeleteHeadship(headshipIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("职务信息成功删除 {} 条").build();
    }

    /**
     * 更新部分职务信息
     * @param headshipUpdatePartialDTO 职务信息参数
     */
    @Override
    public ResultInfo<Integer> modifyHeadshipPartial(HeadshipDTO.UpdatePartialDTO headshipUpdatePartialDTO) {
        Headship headship = headshipConvert.dtoToEntity(headshipUpdatePartialDTO);
        Integer modifySize = headshipRepository.updateHeadshipPartial(headship);
        return ResultInfo.<Integer>builder().data(modifySize).message("职务信息成功更新 {} 条").build();
    }

    /**
     * 更新全部职务信息
     * @param headshipDTO 职务信息参数
     */
    @Override
    public ResultInfo<Integer> modifyHeadship(HeadshipDTO headshipDTO) {
        Headship headship = headshipConvert.dtoToEntity(headshipDTO);
        Integer modifySize = headshipRepository.updateHeadship(headship);
        return ResultInfo.<Integer>builder().data(modifySize).message("职务信息成功更新 {} 条").build();
    }

    /**
     * 查询一条职务信息
     * @param headshipId 职务信息ID
     */
    @Override
    public ResultInfo<HeadshipVO> queryHeadshipById(Long headshipId) {
        Headship headship = headshipRepository.getHeadshipById(headshipId);
        HeadshipVO headshipVO = headshipConvert.entityToVO(headship);
        return ResultInfo.<HeadshipVO>builder().data(headshipVO).message("职务信息查询成功").build();
    }

    /**
     * 分页查询职务信息
     * @param headshipQueryDTO 职务信息参数
     */
    @Override
    public ResultInfo<Page<HeadshipVO>> queryHeadshipPage(HeadshipDTO.QueryDTO headshipQueryDTO) {
        Page<Headship> headshipPage = headshipRepository.getHeadshipPage(headshipQueryDTO);
        Page<HeadshipVO> resultData = headshipConvert.convertPage(headshipPage);
        return ResultInfo.<Page<HeadshipVO>>builder()
                .data(resultData)
                .message("职务信息分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 职务信息 数量
     * @param headshipQueryDTO 职务信息参数
     */
    @Override
    public ResultInfo<Long> queryHeadshipCount(HeadshipDTO.QueryDTO headshipQueryDTO) {
        Long resultCount = headshipRepository.getHeadshipCount(headshipQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("职务信息数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 职务信息
     * @param headshipQueryDTO 职务信息参数
     */
    @Override
    public ResultInfo<List<HeadshipVO>> queryHeadshipList(HeadshipDTO.QueryDTO headshipQueryDTO) {
        List<Headship> headshipList = headshipRepository.getHeadshipList(headshipQueryDTO);
        List<HeadshipVO> resultData = headshipConvert.entityListToVoList(headshipList);
        return ResultInfo.<List<HeadshipVO>>builder()
                .data(resultData)
                .message("职务信息分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
